package com.situ.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.mall.mapper.UserMapper;
import com.situ.mall.pojo.User;
import com.situ.mall.pojo.query.UserQuery;
import com.situ.mall.service.IUserService;
import com.situ.mall.util.LayUITableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(String username, String password) {
        return userMapper.login(username, password);
    }

    @Override
    public boolean selectStatus(User user) {
        return userMapper.selectStatus(user) > 0;
    }

    @Override
    public LayUITableResult selectByPage(UserQuery userQuery) {
        PageHelper.startPage(userQuery.getPage(), userQuery.getLimit());
        List<User> list = userMapper.selectByPage(userQuery);
        PageInfo pageInfo = new PageInfo(list);
        long total = pageInfo.getTotal();
        return LayUITableResult.ok((int)total, list);
    }

    @Override
    public boolean deleteById(Integer id) {
        return userMapper.deleteByPrimaryKey(id) > 0;
    }

    @Override
    public boolean deleteAll(Integer[] ids) {
        return userMapper.deleteAll(ids) > 0;
    }

    @Override
    public boolean updateStatus(Integer id, Byte status) {
        User user = new User();
        user.setId(id);
        user.setStatus(status);
        return userMapper.updateByPrimaryKeySelective(user) > 0;
    }

    @Override
    public boolean insert(User user) {
        return userMapper.insertSelective(user) > 0;
    }

    @Override
    public User selectUpdateUser(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean update(User user) {
        return userMapper.updateByPrimaryKeySelective(user) > 0;
    }
}
